package at.ac.tuwien.ir.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import at.ac.tuwien.ir.additional.ValueComparator;
import at.ac.tuwien.ir.model.Document;
import at.ac.tuwien.ir.model.DocumentWord;
import at.ac.tuwien.ir.model.IndexSize;
import at.ac.tuwien.ir.model.Topic;

public class ProbabilityController implements IProbabilityController {
	
	private ArrayList<Topic> m_topics;
	private IDocumentController m_documentController;
	private String m_documentPath; 
		
	public ProbabilityController(IFileController _fileController, ITokenizer _tokenizer, String _documentsPath)
	{
		m_documentPath = _documentsPath;
		m_documentController = new DocumentController(_fileController, _tokenizer, IndexSize.medium);
		m_documentController.generateDocumentDescription(m_documentPath);
		m_topics = new ArrayList<Topic>();

		m_topics.add(new Topic("misc.forsale", "76057"));
		m_topics.add(new Topic("talk.religion.misc", "83561"));
		m_topics.add(new Topic("talk.politics.mideast", "75422"));
		m_topics.add(new Topic("sci.electronics", "53720"));
		m_topics.add(new Topic("sci.crypt", "15725"));
		m_topics.add(new Topic("misc.forsale", "76165"));
		m_topics.add(new Topic("talk.politics.mideast", "76261"));
		m_topics.add(new Topic("alt.atheism", "53358"));
		m_topics.add(new Topic("sci.electronics", "54340"));
		m_topics.add(new Topic("rec.motorcycles", "104389"));
		m_topics.add(new Topic("talk.politics.guns", "54328"));
		m_topics.add(new Topic("misc.forsale", "76468"));
		m_topics.add(new Topic("sci.crypt", "15469"));
		m_topics.add(new Topic("rec.sport.hockey", "54171"));
		m_topics.add(new Topic("talk.religion.misc", "84177"));
		m_topics.add(new Topic("rec.motorcycles", "104727"));
		m_topics.add(new Topic("comp.sys.mac.hardware", "52165"));
		m_topics.add(new Topic("sci.crypt", "15379"));
		m_topics.add(new Topic("sci.space", "60779"));
		m_topics.add(new Topic("sci.med", "59456"));
	}

	@Override
	public void generateProbabilityFile(String _resultPath)
	{
		if (_resultPath != "" && !_resultPath.endsWith(File.separator))
			_resultPath += File.separator;
		try {
			for (int i = 0; i < m_topics.size(); i++)
			{
				System.out.println("writing " + _resultPath + "_topic" + String.valueOf(i + 1) + "_groupO.txt");
				TreeMap<String, Double> topSimilarities = CalculateTopTenSimilarity(m_topics.get(i));
				FileWriter fileWriter = new FileWriter(_resultPath + "_topic" + String.valueOf(i + 1) + "_groupO.txt");
				PrintWriter writer = new PrintWriter(fileWriter);
				int j = 0;
				for (Map.Entry<String, Double> entry : topSimilarities.entrySet())
				{
					writer.println("topic" + String.valueOf(i + 1) + " Q0 " + entry.getKey() + " " + String.valueOf(j + 1) + " " + String.valueOf(((double)Math.round(entry.getValue() * 100))/100) + " groupO");
					j++;
					if (j==10)
						break;
				}
				writer.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private TreeMap<String, Double> CalculateTopTenSimilarity(Topic topic)
	{
		double k_1 = 1.6;
		double b = 0.75;
		int N = m_documentController.GetDocuments().size();
		double L_ave = m_documentController.GetDocumentLenghtAverage();
		
		HashMap<String, DocumentWord> topicDocumentWords = m_documentController.GetDocumentWords(m_documentPath, topic.get_dir(), topic.get_file(), 0);
		HashMap<String, Double> documentSimilarities = new HashMap<String, Double>();

		for(String topicDocumentWord : topicDocumentWords.keySet()) {
			//int df_tq = topicDocumentWords.get(topicDocumentWord).get_termFrequency();
			int df_t = m_documentController.GetTokenWords().get(topicDocumentWord).getPostingsList().size();
			
			int i = 0;
			for (Document document : m_documentController.GetTokenWords().get(topicDocumentWord).getPostingsList()) {
				if(document.get_documentClass().compareTo(topic.get_dir()) == 0 && document.get_documentName().compareTo(topic.get_file()) == 0) continue;
				
				int tf_td = m_documentController.GetTokenWords().get(topicDocumentWord).getTermFrequencyList().get(i);
				int L_d = document.get_documentSize();

				Double rsv_dt = Math.log10((Math.round((N / df_t)))) * ((k_1 + 1) * tf_td) / (k_1 * ((1 - b) + b * (L_d / L_ave)) + tf_td);  
				
				Double rsv_dt_prev = 0.0;
				String key = document.get_documentClass() + "\\" + document.get_documentName();
				if (documentSimilarities.containsKey(key))
					rsv_dt_prev = documentSimilarities.get(key);
				documentSimilarities.put(key, rsv_dt_prev + rsv_dt);
				i++;
			}
		}
		ValueComparator bvc =  new ValueComparator(documentSimilarities);
        TreeMap<String,Double> sorted_documentSimilarities = new TreeMap<String,Double>(bvc);
        sorted_documentSimilarities.putAll(documentSimilarities);
        
		return sorted_documentSimilarities;
	}
}
